package com.example.demo.service;

import com.example.demo.model.Teacher;
import com.example.demo.model.User;
import com.example.demo.model.College;
import com.example.demo.repository.TeacherRepository;
import com.example.demo.repository.UserRepository;
import com.example.demo.repository.CollegeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
import java.time.LocalDateTime;
import java.util.stream.Collectors;

@Service
public class TeacherService {
    @Autowired
    private TeacherRepository teacherRepository;
    
    @Autowired
    private UserRepository userRepository;
    
    @Autowired
    private CollegeRepository collegeRepository;

    public Page<Teacher> getAllTeachers(int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        Page<Teacher> teacherPage = teacherRepository.findAll(pageable);
        // 为每个教师填充用户信息和关联信息
        return teacherPage.map(this::populateTeacherUserInfo);
    }
    
    // 获取所有教师用于导出
    public List<Teacher> getAllTeachersForExport() {
        List<Teacher> teachers = teacherRepository.findAll();
        // 为每个教师填充用户信息和关联信息
        return teachers.stream().map(this::populateTeacherUserInfo).collect(java.util.stream.Collectors.toList());
    }
    
    // 添加搜索教师的方法
    public Page<Teacher> searchTeachers(String teacherNo, String name, int page, int size) {
        Pageable pageable = PageRequest.of(page, size);
        Page<Teacher> teacherPage = teacherRepository.findByTeacherNoContainingAndUserNameContaining(
            teacherNo.isEmpty() ? null : teacherNo,
            name.isEmpty() ? null : name,
            pageable
        );
        // 为每个教师填充用户信息和关联信息
        return teacherPage.map(this::populateTeacherUserInfo);
    }
    
    private Teacher populateTeacherUserInfo(Teacher teacher) {
        // 填充用户信息
        Optional<User> userOpt = userRepository.findById(teacher.getUserId());
        if (userOpt.isPresent()) {
            User user = userOpt.get();
            teacher.setName(user.getRealName());
            teacher.setCreatedAt(user.getCreatedAt());
            teacher.setGender(user.getGender() != null ? user.getGender().name() : ""); // 设置性别信息
        }
        
        // 填充学院信息
        if (teacher.getCollegeId() != null) {
            Optional<College> collegeOpt = collegeRepository.findById(teacher.getCollegeId());
            if (collegeOpt.isPresent()) {
                teacher.setCollegeName(collegeOpt.get().getName());
            }
        }
        
        return teacher;
    }

    public Optional<Teacher> getTeacherById(Long id) {
        Optional<Teacher> teacherOpt = teacherRepository.findById(id);
        if (teacherOpt.isPresent()) {
            Teacher teacher = teacherOpt.get();
            populateTeacherUserInfo(teacher);
            return Optional.of(teacher);
        }
        return teacherOpt;
    }

    public Teacher saveTeacher(Teacher teacher) {
        return teacherRepository.save(teacher);
    }

    public void deleteTeacher(Long id) {
        teacherRepository.deleteById(id);
    }

    public Optional<Teacher> getTeacherByTeacherNo(String teacherNo) {
        return teacherRepository.findByTeacherNo(teacherNo);
    }

    public Optional<Teacher> getTeacherByUserId(Long userId) {
        return teacherRepository.findByUserId(userId);
    }
}